package edu.buaa.act.helper;

import edu.buaa.act.model.GPSPoint;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by T7810 on 2017/6/7.
 */
public class CsvGPSFileReader
{
    public static final int CAR_ID = 0;
    public static final int TIMESTAMP = 1;
    public static final int LATITUDE = 2;
    public static final int LONGITUDE = 3;

    public static List<GPSPoint> readLargeFile(File file, String sep, int... fields) throws IOException
    {
        int[] map = new int[fields.length];
        for(int i=0; i<fields.length; i++){
            map[fields[i]] = i;
        }

        BufferedReader ir = new BufferedReader(new FileReader(file), 100_000);
        List<GPSPoint> list = new ArrayList<>(1007_0000);
        String lineTxt;
        while((lineTxt = ir.readLine()) != null) {
            String[] arr = lineTxt.split(sep);
            long carNo = Long.parseLong(arr[map[CAR_ID]]);
            long timeStampSecond = Integer.parseInt(arr[map[TIMESTAMP]]);
            double latitude = Double.parseDouble(arr[map[LATITUDE]]);
            double longitude = Double.parseDouble(arr[map[LONGITUDE]]);
            list.add(new GPSPoint(carNo, timeStampSecond*1000, longitude, latitude, 0, 0));
        }
        ir.close();
        return list;
    }

}
